/*
 * Copyright 2010 EES GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.eesgmbh.gimv.samples.googlechartapi.client.infobox;

import org.eesgmbh.gimv.client.event.SetImageUrlEvent;
import org.eesgmbh.gimv.client.event.SetImageUrlEventHandler;
import org.eesgmbh.gimv.client.event.ViewportMouseMoveEvent;
import org.eesgmbh.gimv.client.event.ViewportMouseMoveEventHandler;
import org.eesgmbh.gimv.shared.util.Point;

import com.google.gwt.event.shared.HandlerManager;

/**
 *
 * InfoBox Presenter to display the current mouse position and the
 * link to the chart image.
 *
 * @author Sascha Hagedorn - EES GmbH - s.hagedorn@ees-gmbh.de
 *
 */
public class InfoBoxPresenter {

	public interface View {
		void setUrl(String urlString);
		void setMousePositionLabel(Point position);
	}

	private final View view;

	public InfoBoxPresenter(HandlerManager handlerManager, View view) {
		this.view = view;

		InfoBoxPresenterEventHandler eventHandler = new InfoBoxPresenterEventHandler();

		handlerManager.addHandler(SetImageUrlEvent.TYPE, eventHandler);
		handlerManager.addHandler(ViewportMouseMoveEvent.TYPE, eventHandler);
	}

	private void onMouseMove(ViewportMouseMoveEvent event) {
		this.view.setMousePositionLabel(new Point((double)event.getGwtEvent().getX(), (double)event.getGwtEvent().getY()));
	}

	private void onSetImageUrl(SetImageUrlEvent event) {
		this.view.setUrl(event.getUrl());
	}

	private class InfoBoxPresenterEventHandler implements SetImageUrlEventHandler, ViewportMouseMoveEventHandler {

		public void onMouseMove(ViewportMouseMoveEvent event) {
			InfoBoxPresenter.this.onMouseMove(event);
		}

		public void onSetImageUrl(SetImageUrlEvent event) {
			InfoBoxPresenter.this.onSetImageUrl(event);

		}

	}
}
